package com.jiepos.mpos.core.service;

import java.sql.Timestamp;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.core.entity.BaseEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.util.StringUtils;

 
/**
 * 业务层父类 
 * @author 伟峰
 *
 */
@Service
public class BaseService {
	/** 自己的日志 */
	private final Logger currLog = LoggerFactory.getLogger(BaseService.class);

	/** 子类的日志 */
	public final Logger log = LoggerFactory.getLogger(getClass()); 

	/**
	 * 拼装过滤机构数据权限sql
	 * 
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	public Pagination getInsAuthSql(HttpServletRequest request, Pagination pagination, String prefix, Map map) {
		pagination.queryString = this.getInsAuthSql(request, pagination.queryString, prefix, map);
		return pagination;
	}

	/**
	 * 获取机构权限拼装sql
	 * 
	 * @param request
	 * @param sq1
	 * @param prefix
	 * @return
	 */
	public String getInsAuthSql(HttpServletRequest request, String sql, String prefix, Map map) {
		if (!StringUtils.isBlank(prefix)) {
			prefix = prefix + ".";
		} else {
			prefix = "";
		}
		String and = "and";
		if (map != null) {
			and = StringUtils.objToString(String.valueOf(map.get("and")));
		}
		StringBuilder sqlBuilder = new StringBuilder();
		// 获取登陆用户名
		String loginName = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_USER));
  
		String ins_uuid = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_UUID));
		
		String ins_phone = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_PHONE));
		// 如果是机构信息，则查询机构表
		// 如果此用户是属于某个机构，则获取当前机构及下级机构代码
	 	sqlBuilder.append(" " + and + " " + prefix + "ins_uuid in ");
		sqlBuilder.append(" (select tt.ins_uuid from "+Table.T_APP_INS_INF+" tt ");   
		sqlBuilder.append(" start with tt.ins_uuid='" + ins_uuid + "'");
		sqlBuilder.append(" connect by prior tt.ins_uuid = tt.sup_ins_uuid)"); 
		sql = sql + sqlBuilder.toString();
		return sql;
	}
	
	
	
	/**
	 * 拼装会员手机号过滤数据权限sql
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	public Pagination getCustomAuthSql(HttpServletRequest request, Pagination pagination, String prefix, Map map) {
		pagination.queryString = this.getCustomAuthSql(request, pagination.queryString, prefix, map);
		return pagination;
	}
	
	
	/**
	 * 获取会员权限拼装sql
	 * 
	 * @param request
	 * @param sq1
	 * @param prefix
	 * @return
	 */
	public String getCustomAuthSql(HttpServletRequest request, String sql, String prefix, Map map) {
		if (!StringUtils.isBlank(prefix)) {
			prefix = prefix + ".";
		} else {
			prefix = "";
		}
		String and = "and";
		if (map != null) {
			and = StringUtils.objToString(String.valueOf(map.get("and")));
		}
		StringBuilder sqlBuilder = new StringBuilder();
		// 获取登陆用户名
  
		String ins_uuid = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_UUID));
		
		String ins_phone = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_PHONE));
		// 如果是机构信息，则查询机构表
		// 如果此用户是属于某个机构，则获取当前机构及下级机构代码
	 	sqlBuilder.append(" " + and + " " + prefix + "custom_uuid in ");
		sqlBuilder.append(" (select tt.custom_uuid from "+Table.T_APP_CUSTOM_INF+" tt ");   
		sqlBuilder.append(" start with tt.login_id='" + ins_phone + "'");
		sqlBuilder.append(" connect by prior tt.custom_uuid = tt.sup_custom_uuid)"); 
		sql = sql + sqlBuilder.toString();
		return sql;
	}
	


	/**
	 * 拼装排序sql
	 * 
	 * @param pagination
	 * @return
	 */
	public Pagination getOrderSql(Pagination pagination, String prefix) {
		if (!StringUtils.isBlank(prefix)) {
			prefix = prefix + ".";
		} else {
			prefix = "";
		}
		pagination.queryString = pagination.queryString + " and " + prefix + SystemConstant.REC_ST + "!='"
				+ SystemConstant.DEL_STATUS + "' order by " + prefix + "rec_crt_ts desc";
		log.debug("pagination.queryString : " + pagination.queryString);
		return pagination;
	}

	/**
	 * 拼装排序sql
	 * 
	 * @param pagination
	 * @return
	 */
	public String getOrderSql(String sql, String prefix) {
		if (!StringUtils.isBlank(prefix)) {
			prefix = prefix + ".";
		} else {
			prefix = "";
		}
		sql = sql + " and " + prefix + SystemConstant.REC_ST + "!='" + SystemConstant.DEL_STATUS + "' order by "
				+ prefix + "rec_crt_ts desc";
		log.debug("sql : " + sql);
		return sql;
	}

	/**
	 * 公共部分的sql
	 * @param prefix 别名
	 * @return
	 */
	public StringBuffer getInitSql(String prefix){
		if (!StringUtils.isBlank(prefix)) {
			prefix = prefix + ".";
		} else {
			prefix = "";
		}
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append("to_char(").append(prefix).append("rec_crt_ts, 'yyyy-mm-dd hh24:mi:ss') rec_crt_ts,")
		.append("to_char(").append(prefix).append("rec_upd_ts, 'yyyy-mm-dd hh24:mi:ss') rec_upd_ts,")
		.append(prefix).append("rec_upd_usr_id");
		return sql;
	}
	
	/**
	 * 保存公用参数方法
	 * @param baseEntity 实体对象
	 * @param userMap 登录用户信息集合
	 * @param type 表示是修改还是新增，新增时为null，修改时为update
	 * @return BaseEntity
	 */
	public BaseEntity saveCommonData(BaseEntity baseEntity,Map userMap,String type){
		String loginId = String.valueOf(userMap.get(SystemConstant.SESSION_LOGIN_USER));
		if("update".equals(type)){
			baseEntity.setLast_oper_in(SystemConstant.OPER_UPDATE);
		}else{
			baseEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			baseEntity.setLast_oper_in(SystemConstant.OPER_ADD);
		}
	
		baseEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
		baseEntity.setRec_st(SystemConstant.NORMAL_STATUS);
	
		baseEntity.setRec_upd_usr_id(loginId);
		return baseEntity;
	}
	
	/**
	 * 拼装过滤机构数据权限sql(只显示本级机构及直属机构)
	 * 
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	public Pagination getDirectInsAuthSql(HttpServletRequest request, Pagination pagination, String prefix, Map map) {
		pagination.queryString = this.getDirectInsAuthSql(request, pagination.queryString, prefix, map);
		return pagination;
	}

	/**
	 * 获取机构权限拼装sql(只显示本级机构及直属机构)
	 * 
	 * @param request
	 * @param sq1
	 * @param prefix
	 * @return
	 */
	public String getDirectInsAuthSql(HttpServletRequest request, String sql, String prefix, Map map) {
		
		String and = "and";
		if (map != null) {
			and = StringUtils.objToString(String.valueOf(map.get("and")));
		}
		StringBuilder sqlBuilder = new StringBuilder();
		// 获取登陆用户登录名
		String loginName = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_USER));
		String ins_uuid = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_UUID));
		/*用户机构的等级*/
		int loginUserInsLevel = NumberUtils.toInt(StringUtils.objToString(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_LEVEL)));
		if(loginUserInsLevel == 1){
			return getInsAuthSql(request, sql, prefix, map);
		}else{
			if (!StringUtils.isBlank(prefix)) {
				prefix = prefix + ".";
			} else {
				prefix = "";
			}
			/*查询用户的机构的直属机构*/
			sqlBuilder.append(" " + and + " (" + prefix + "ins_uuid in ");
			sqlBuilder
					.append("(SELECT INS_UUID FROM " + Table.T_APP_INS_INF + " WHERE SUP_INS_UUID = '"+ins_uuid+"')");
			sqlBuilder.append(" or " + prefix + "ins_uuid ='"+ins_uuid+"')");
			
			sql = sql + sqlBuilder.toString();
			return sql;
		}
		
	}
}
